package scan.common;

import scan.transaction.XAResourceManager;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class GlobalTransactionInfo {
    public static final ThreadLocal<String> globalTransactionId = new ThreadLocal<>();

    public static final ThreadLocal<List<XAResourceManager>> xidListMap = new ThreadLocal<>();

    public static final ConcurrentHashMap<String, Boolean> prepareStatus = new ConcurrentHashMap<>();

    public static void remove() {
        prepareStatus.remove(globalTransactionId.get());
        globalTransactionId.remove();
        List<XAResourceManager> xaResourceManagers = xidListMap.get();
        for (XAResourceManager xaResourceManager: xaResourceManagers) {
            // 对每一个开启的 RM 进行关闭
            xaResourceManager.close();
        }
        xidListMap.remove();
    }
}
